package com.ad.pet.app.upgrade;

import android.app.Activity;

import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.data.injector.PerActivity;
import com.ad.pet.models.remote.Upgrade;

import javax.inject.Inject;

/**
 * Created by SEELE on 2018/4/9.
 */
@PerActivity
public class UpgradePresenter implements UpgradeContract.Presenter {

    private ApiService mApiService;
    private UpgradeContract.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;

    @Inject
    public UpgradePresenter(ApiService apiService,
                            Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }

    @Override
    public void onCreate(UpgradeContract.View view) {
        this.mView = view;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
        mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }

    @Override
    public void upgrade(String versionCode) {
        mRxDisposable.add(mApiService.upGrade(versionCode)
                .subscribe(new AppSubscriber<Upgrade>(mView) {
                    @Override
                    public void next(Upgrade keyWord) {
                        mView.upgradeSuccess(keyWord);
                    }
                }));
    }
}
